*******************************************************************
*Replication code for
*	Article: COVID 19 infection induces higher trust in strangers
*	Journal: PNAS
*	Authors: Diego Gambetta and Davide Morisi
*******************************************************************

*PREPARE DATASET FOR PANEL ANALYSIS

*Set working directory
cd "[your working directory]"

*open data
use "Replication\Data\swg_panel_replication", clear

*standardized measure w1 recall
drop st_trust_strangerW1
egen st_trust_strangerW1 = std(trust_stranger)
sum st_trust_strangerW*

*RENAMING
*DV: trust_stranger
rename trust_stranger trust_strangerW1
rename w2_trust_stranger trust_strangerW2
rename w3_trust_stranger trust_strangerW3
*trust general
rename trust_gen trust_genW1
rename w2_trust_gen trust_genW2
rename w3_trust_gen trust_genW3
*trust italians
rename w2_trust_it01 trust_itW2
rename w3_trust_it01 trust_itW3
*trust neighbours
rename trust_neighbour trust_neighbourW1
rename w2_trust_neighbour trust_neighbourW2
rename w3_trust_neighbour trust_neighbourW3
*trust police
rename trust_police trust_policeW1
rename w2_trust_police trust_policeW2
rename w3_trust_police trust_policeW3

*covid symptoms
rename covid_ind covid_indW1
rename w2_covid_ind covid_indW2
rename w3_covid_ind covid_indW3
*people with covid in network - family
rename covid_family4 covid_family4_W1
rename w2_covid_family4 covid_family4_W2
rename w3_covid_family4 covid_family4_W3
*people with covid in network - friends/acquaintance
rename covid_friends4 covid_friends4_W1
rename w2_covid_acq_fr4 covid_friends4_W2
rename w3_covid_acq_fr4 covid_friends4_W3
*people with covid in network - colleagues
rename covid_colleague4 covid_col4_W1
rename w2_covid_col4 covid_col4_W2
rename w3_covid_col4 covid_col4_W3

*perceived eco situation
rename w1_perceived_eco2 perceived_eco2W1
rename w2_perceived_eco2 perceived_eco2W2
rename w3_perceived_eco2 perceived_eco2W3
*received help
rename w2_help_by_vol_str help_by_volstrW1
rename w3_help_by_vol_str help_by_volstrW3

*excess mortality
rename incr_mar_apr2020 excessW1
rename incr_sep_oct2020 excessW2
rename incr_dec_jan2021 excessW3
rename incr_mar_apr2020_w5 excess_w5_W1
rename incr_sep_oct2020_w5 excess_w5_W2
rename incr_dec_jan2021_w5 excess_w5_W3

*household
rename w1_live_alone live_aloneW1
rename w2_live_alone live_aloneW2
rename w3_live_alone live_aloneW3


************************
*Reshape dataset as long

*reshape
reshape long trust_strangerW trust_genW pca1_W pca_full1_W ///
trust_itW trust_neighbourW trust_policeW trust_govW ///
covid_family4_W covid_friends4_W covid_col4_W ///
perceived_eco2W help_by_volstrW live_aloneW ///
covid_indW covid_confirmedW excessW excess_w5_W, i(TEL_numeric) j(wave)

******************
*WITHIN-BETWEEN MODEL

*IDV - averages of independent variables
*covid symptoms
bys TEL_numeric: egen meancovid=mean(covid_indW)
fre meancovid
gen meancovidr = meancovid*3
fre meancovid meancovidr if panel==4
*1= suffered once, 2=suffered twice, 3=suffered each time
bys wave: fre covid_indW* if meancovid>0.3 & meancovid<0.5
bys wave: fre covid_indW* if meancovid==1
*recode so that 1 if you suffered at least once in the panel
fre meancovidr
gen meancovid01 = meancovidr if meancovidr==0
replace meancovid01 = 1 if meancovidr>0 & meancovidr!=.
label de meancovid01 0"no symptoms" 1"symptoms at least once"
label val meancovid01 meancovid01
fre meancovidr meancovid01

*confirmed symptoms
fre covid_confirmedW
bys TEL_numeric: egen meancovid_conf=mean(covid_confirmedW)
gen meancovid_confr = meancovid_conf*3
fre meancovid_conf meancovid_confr if panel==4

*network family
bys TEL_numeric: egen meancovid_family4=mean(covid_family4_W)
*network friends
bys TEL_numeric: egen meancovid_friends4=mean(covid_friends4_W)
*network colleagues
bys TEL_numeric: egen meancovid_col4=mean(covid_col4_W)

*perceived eco
bys TEL_numeric: egen meanperceived2_eco=mean(perceived_eco2W)
gen meanperceived2_eco01 = meanperceived2_eco if meanperceived2_eco==0
replace meanperceived2_eco01 = 1 if meanperceived2_eco>0 & meanperceived2_eco!=.
label val meanperceived2_eco01 w3_perceived_eco2
fre meanperceived2_eco meanperceived2_eco01 perceived_eco2W

*excess mortality
bys TEL_numeric: egen meanexcess=mean(excessW)
bys TEL_numeric: egen meanexcess_w5=mean(excess_w5_W)
egen min = min(meanexcess_w5)
egen max = max(meanexcess_w5)
gen meanexcess_w501 = (meanexcess_w5-min)/(max-min) // rescaled from 0 to 1
drop min max

*live alone
bys TEL_numeric: egen meanlive_alone=mean(live_aloneW)
gen meanlive_alone01 = meanlive_alone if meanlive_alone==0
replace meanlive_alone01 = 1 if meanlive_alone>0 & meanlive_alone!=.
label val meanlive_alone01 w3_live_alone
fre meanlive_alone meanlive_alone01 live_aloneW


***
*differences

*covid symptoms
gen covid_diff=covid_indW-meancovid
*confirmed
gen covid_conf_diff=covid_confirmedW-meancovid_conf
*network family
gen covid_fam4diff = covid_family4_W-meancovid_family4
*network friends
gen covid_fr4diff = covid_friends4_W-meancovid_friends4
*network colleagues
gen covid_col4diff = covid_col4_W-meancovid_col4
*excess death
gen excess_diff = excessW-meanexcess
gen excess_w5_diff = excess_w5_W-meanexcess_w5
egen min = min(excess_w5_diff)
egen max = max(excess_w5_diff)
gen excess_w5_diff01 = (excess_w5_diff-min)/(max-min)
gen excess_w5_diff01a = excess_w5_diff01*1.3 // same scale as other covid differences
sum excess_w5_diff01*
drop min max

***
*help in w1 and w3
fre help_by_volstrW
bys TEL: egen meanhelp_by_volstr=mean(help_by_volstrW) if wave!=2
fre meanhelp_by_volstr
recode meanhelp_by_volstr 0.5=1
*help before pandemic
gen w2_help_by_volstrpre = 0 if w2_help_by_volpre!=.
replace w2_help_by_volstrpre = 1 if w2_help_by_volpre==1 | w2_help_by_strpre==1
fre w2_help_by_volstrpre


*standardized DVs
foreach var of varlist trust_neighbourW trust_policeW trust_strangerW trust_itW pca1_W pca_full1_W {
	egen st_`var'=std(`var')
}

*rescale pca vars from 0 to 1
foreach var of varlist pca1_W pca_full1_W  {
	egen min_`var' = min(`var')
	egen max_`var' = max(`var')
	gen n`var' = (`var' - min_`var') / (max_`var' - min_`var')
	sum n`var'
	drop min_`var' max_`var'
}

****
save "Replication\Data\swg_panel_long.dta", replace

